﻿using System;
using System.ComponentModel.DataAnnotations;
using FreeSql;
using EAF.Utility;
using EAF.DBC;
using App.Utility;
using App.Model.Main;
using System.Collections.Generic;

namespace App.DTO.Main
{
    /// <summary>
    /// 系统用户 业务字段
    /// </summary>
    public class SysUserBaseDto
    {
        /// <summary>
        /// 编号
        /// </summary> 
        [Display(Name = "编号")]
        public virtual int id { get; set; }

        /// <summary>
        /// 登陆账号
        /// </summary>
        [Display(Name = "登陆账号")]
        [NotNull]
        [StrMaxLen(20)]
        public string account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Display(Name = "密码")]
        [NotNull]
        [StrMaxLen(50)]
        public string password { get; set; }

        // /// <summary>
        // /// 密码混淆码
        // /// </summary>
        // [Display(Name = "密码混淆码")]
        // [StrMaxLen(50)]
        // public string salt { get; set; }

        /// <summary>
        /// 权限id
        /// </summary>
        [Display(Name = "权限id")]
        public List<int> roleIds { get; set; }

        /// <summary>
        /// 默认子平台
        /// </summary>
        [Display(Name = "默认子平台")]
        public int defPlatform { get; set; }

        /// <summary>
        /// 子系统平台id
        /// </summary>
        [Display(Name = "子系统平台id")]
        public List<int> platformIds { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [Display(Name = "真实姓名")]
        [StrMaxLen(20)]
        public string realName { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        [Display(Name = "昵称")]
        [NotNull]
        [StrMaxLen(20)]
        public string nickName { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        [Display(Name = "头像")]
        [NotNull]
        [StrMaxLen(200)]
        public string headIcon { get; set; }

        /// <summary>
        /// 0未知 1男 2女
        /// </summary>
        [Display(Name = "0未知 1男 2女")]
        public int gender { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        [Display(Name = "生日")]
        public DateTime? birthday { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        [Display(Name = "手机号")]
        [NotNull]
        [StrMaxLen(20)]
        public string mobilePhone { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [Display(Name = "邮箱")]
        [NotNull]
        [StrMaxLen(20)]
        public string email { get; set; }

        /// <summary>
        /// 微信号
        /// </summary>
        [Display(Name = "微信号")]
        [NotNull]
        [StrMaxLen(20)]
        public string weChat { get; set; }

        /// <summary>
        /// 是不是超级管理员
        /// </summary>
        [Display(Name = "是不是超级管理员")]
        public bool isAdministrator { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        [Display(Name = "最后登录时间")]
        public DateTime? lastLoginTime { get; set; }

        /// <summary>
        /// 绑定账户id
        /// </summary>
        [Display(Name = "绑定账户id")]
        public int? userId { get; set; }

        /// <summary>
        /// 用户身份
        /// </summary>
        [Display(Name = "用户身份")]
        public int? userIdentity { get; set; }

        /// <summary>
        /// 数据版本 业务中无需显示维护值 用于乐观锁添加传0 更新传详情接口的值
        /// </summary>
        [Display(Name = "数据版本")]
        public virtual int version { get; set; }
    }

    /// <summary>
    /// 系统用户 分页请求json实体类 这里字段请使用首字母小写
    /// </summary>
    public class SysUserPageReq : DtoPageReqBase
    {
        /// <summary>
        /// 常用字段搜索 默认根据支持CreaterName模糊查询
        /// </summary>
        [Display(Name = "常用字段搜索")]
        public string searchValue { get; set; }

        /// <summary>
        /// 查询参数处理
        /// </summary>
        /// <param name="dbc">表所在库上下文</param>
        /// <returns></returns>
        public ISelect<SysUser> Query(BaseDbContext dbc)
        {
            var query = dbc.Select<SysUser>();

            //显示模式处理
            if (showMode != ShowMode.Normal)
            {
                query.DisableGlobalFilter();
                if (showMode == ShowMode.Deleted)
                {
                    query = query.Where(w => w.Deleted == true);
                }
            }

            //前端不传入whereDynamicFilter高级过滤 特殊条件过滤
            if (whereDynamicFilter.IsNull())
            {
                //这里处理常用字段查询处理
                query = query.WhereIf(searchValue.HasValue(), w => w.CreaterName.Contains(searchValue));
            }
            else
            {
                //高级过滤查询
                var whereObj = dbc.ParseWhereDynamicFilter(whereDynamicFilter);
                if (whereObj != null)
                {
                    query = query.WhereDynamicFilter(whereObj);
                }
            }
            //前端不传排序字符串 则按id降序
            if (orderBy == null)
            {
                query = query.OrderByDescending(f => f.Id);
            }
            else
            {
                query = query.OrderBy(orderBy);
            }

            return query;
        }

    }

    /// <summary>
    /// 系统用户 请求json实体类
    /// </summary>
    public class SysUserReq : SysUserBaseDto
    {
        /// <summary>
        /// json实例转换为db实例 就是将同名字段赋值到dbEntity 相当于更新
        /// </summary>
        /// <param name="dbEntity">表实例</param>
        /// <returns></returns>
        public SysUser ConvertDbEntity(SysUser dbEntity)
        {
            this.AutoMapObjectTo(dbEntity);

            //无法自动绑定的在此手动绑定


            return dbEntity;
        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string description { get; set; }

        /// <summary>
        /// 排序码
        /// </summary>
        [Display(Name = "排序码")]
        public virtual int? sortNo { get; set; }

        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool valid { get; set; }

        #endregion

    }

    /// <summary>
    /// 系统用户 响应json实体类
    /// </summary>
    public class SysUserResp : SysUserBaseDto
    {
        public SysUserResp() { }

        /// <summary>
        /// db实例转换json实例 就是将同名字段赋值到当前实例 相当于更新复制
        /// </summary>
        /// <param name="dbEntity">db实例</param>
        public SysUserResp(SysUser dbEntity)
        {
            dbEntity.AutoMapObjectTo(this);

            //无法自动绑定的在此手动绑定

        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string description { get; set; }

        /// <summary>
        /// 排序数字
        /// </summary>
        [Display(Name = "排序数字")]
        public virtual int? sortNo { get; set; }

        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool valid { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        [Display(Name = "创建者ID")]
        public virtual int? createrId { get; set; }

        /// <summary>
        /// 创建人名
        /// </summary>
        [Display(Name = "创建人名")]
        public virtual string createrName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        public virtual DateTime createTime { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        [Display(Name = "修改时间")]
        public virtual DateTime? updateTime { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        [Display(Name = "是否删除")]
        public virtual bool deleted { get; set; }

        /// <summary>
        /// 删除时间
        /// </summary>
        [Display(Name = "删除时间")]
        public virtual DateTime? deleteTime { get; set; }

        #endregion

    }

    /// <summary>
    /// 登录请求
    /// </summary>
    public class AdminLoginReq
    {
        /// <summary>
        /// 登录账号
        /// </summary>
        [NotNull]
        public virtual string account { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [NotNull]
        public virtual string password { get; set; }

        /// <summary>
        /// 子系统id
        /// </summary> 
        public virtual int platformId { get; set; }
    }

    /// <summary>
    /// 登录响应
    /// </summary>
    [Serializable]
    public class AdminLoginResp
    {
        /// <summary>
        /// uid 调用接口需要
        /// </summary>
        public int uId { get; set; }
        /// <summary>
        /// 调接口用token
        /// </summary>
        public string token { get; set; }
        /// <summary>
        /// token失效时用于刷新token 每次失效用它刷新 每次请注意更新
        /// </summary>
        public string refToken { get; set; }
        /// <summary>
        /// 管理员信息
        /// </summary>
        public SysUserResp adminInfo { get; set; }
        /// <summary>
        /// 管理员模块权限
        /// </summary>
        public List<ModuleAuthResp> authInfo { get; set; }
        /// <summary>
        /// 平台信息
        /// </summary>
        public List<SysPlatformResp> platformInfo { get; set; }
        /// <summary>
        /// 默认子系统id
        /// </summary>
        public int platformId { get; set; }
    }

    /// <summary>
    /// 授权模块 用于登录响应授权的模块信息
    /// </summary> 
    public class ModuleAuthResp
    {
        /// <summary>
        /// 模块信息
        /// </summary> 
        public virtual ModuleAuthItem entity { get; set; }

        /// <summary>
        /// 子模块
        /// </summary> 
        public virtual List<ModuleAuthResp> childs { get; set; }


        public class ModuleAuthItem
        {
            public virtual int id { get; set; }
            /// <summary>
            /// 页面路由url
            /// </summary> 
            public virtual string index { get; set; }
            /// <summary>
            /// 图标
            /// </summary> 
            public virtual string icon { get; set; }
            /// <summary>
            /// 名字
            /// </summary> 
            public virtual string name { get; set; }
            /// <summary>
            /// 英文名字
            /// </summary> 
            public virtual string enName { get; set; }

            /// <summary>
            /// 接口授权
            /// </summary>
            public List<string> auth { get; set; }
            /// <summary>
            /// 描述
            /// </summary> 
            public virtual string desc { get; set; }

        }

    }

    /// <summary>
    /// 刷新token
    /// </summary>
    public class RefreshTokenReq
    {
        /// <summary>
        /// uid开发环境可为空
        /// </summary>
        public int uId { get; set; }
        /// <summary>
        /// 原token
        /// </summary>
        [NotNull]
        public virtual string token { get; set; }
        /// <summary>
        /// 刷新token 此token再使用后会更新 每次刷新后保存
        /// </summary>
        [NotNull]
        public virtual string refToken { get; set; }

    }

    /// <summary>
    /// 修改密码请求
    /// </summary>
    public partial class RestPasswordReq
    {
        /// <summary>
        /// 登录账号
        /// </summary>
        [NotNull]
        public virtual string account { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [NotNull]
        public virtual string password { get; set; }
        /// <summary>
        /// 新密码
        /// </summary>
        [NotNull]
        public virtual string newPassword { get; set; }

    }

}


